package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;

public class LoanController
{
    private LoanRegister loanRegister;// instance variables - replace the example below with your own
    private DvdRegister register;
    private AddressBook addressBook;

    /**
     * Constructor for objects of class LoandCtr
     */
    public LoanController()
    {
        loanRegister = LoanRegister.getInstance();
        addressBook = AddressBook.getInstance();
        register = DvdRegister.getInstance();
    }
    
    public void setLoanRegister(int id, String borrowDate, int period,String dvdTitle, String personID)
    {
        Copy c = register.findDVD(dvdTitle).findUnrentedCopy();
        loanRegister.setLoan(id, borrowDate, period, c, addressBook.findPerson(personID));
    }
    
    public Copy findUnrentedCopy(String dvdTitle)
    {
        return register.findDVD(dvdTitle).findUnrentedCopy();
    }
    
    public Person findPersonMistake(String personID)
    {
        return addressBook.findPerson(personID);
    }
    
    public ArrayList<Loan> getLoanRegister()
    {
        return loanRegister.getLoan();
    }
    
    public void deleteLoan(int id)
    {
        loanRegister.deleteLoan(id);
    }
    
    public Loan findLoan(int id)
    {
        return loanRegister.findLoan(id);
    }
    
    public void editLoan(int id, String borrowDate, int period)
    {
        loanRegister.editLoan(findLoan(id),  borrowDate, period);
    }
    
    public String returnLoan(int id,int howMuchWasRented)
    {
        boolean t = false;
        if(howMuchWasRented>findLoan(id).getPeriod()){
        t=true;}
        String s="";
        //Loan loan1=findLoan(id);
        findLoan(id).getPerson().deleteLoan(findLoan(id));
        findLoan(id).getCopy().unrentCopy();
        findLoan(id).getCopy().deleteLoan();
        loanRegister.deleteLoan(findLoan(id));
        if(t)
        {s="Extra money has to be paid"+'\n';
        }
        else
        {
            s="The DVD was returned in time"+ '\n';
        }
        return s;
    }
    
    public String printLoans()
    {
        return loanRegister.printLoans();
    }
}